from urllib.parse import urlparse


def get_domain(url):
    try:
        # 解析URL
        parsed_url = urlparse(url)
        # 提取netloc部分（域名部分）
        domain = parsed_url.netloc
        # 如果 URL 没有以 http/https 开头，urlparse 会认为它是相对路径，此时 netloc 为空
        if not domain:
            raise ValueError("Invalid URL")
        return domain
    except Exception as e:
        return f"Error: {str(e)}"

# def get_domain2(url):
#     #print('step=> get_domain function')
#     extracted = tldextract.extract(url)
#     #subdomain = extracted.subdomain
#     domain = extracted.domain
#     suffix = extracted.suffix
#     full_domain = ".".join(part for part in [domain, suffix] if part)
#     return full_domain


# 示例
# url = "https://www.baidu.com/path/to/page"
# print(get_domain(url))
